/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.LocationType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import utils.SQLServerDB;

/**
 *
 * @author Hai Muoi Thang Sau
 */
public class LocationTypeDAO {

    final String SQL_CREATE = "INSERT INTO LOCATIONTYPE VALUES (?,?)";
    final String SQL_READALL = "SELECT * FROM LOCATIONTYPE";
    final String SQL_READBYID = "SELECT * FROM LOCATIONTYPE WHERE ID = ?";
    final String SQL_UPDATE = "UPDATE LOCATIONTYPE SET NAME = ?, Description = ? WHERE ID = ?";
    final String SQL_DELETE = "DELETE FROM Location WHERE ID=?";
    private Connection conn;
    private String lastError;

    public boolean create(LocationType p) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_CREATE);
            pr.setString(1, p.getName());
            pr.setString(2, p.getDescript());
            if (pr.executeUpdate() > 0) {
                setLastError("Create Successful!");
                return true;
            } else {
                setLastError("Error: Create Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public ArrayList<LocationType> readAll() {
        conn = SQLServerDB.getConn();
        ArrayList<LocationType> locationTypes = new ArrayList<>();
        try {

            PreparedStatement pr = conn.prepareStatement(SQL_READALL);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                LocationType p = new LocationType();
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("Name"));
                p.setDescript(rs.getString("Description"));
                locationTypes.add(p);
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return locationTypes;
    }

    public LocationType readByID(int id) {
        conn = SQLServerDB.getConn();
        LocationType l = new LocationType();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_READBYID);
            pr.setInt(1, id);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                l.setId(rs.getInt("ID"));
                l.setName(rs.getString("Name"));
                l.setDescript(rs.getString("Description"));
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return l;

    }

    public boolean update(LocationType p) {
        conn = SQLServerDB.getConn();
        try {
            
            PreparedStatement pr = conn.prepareStatement(SQL_UPDATE);
            pr.setString(1, p.getName());
            pr.setString(2, p.getDescript());
            pr.setInt(3, p.getId());
            if (pr.executeUpdate() > 0) {
                setLastError("Update Successful!");
                } else {
                setLastError("Error: Update Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public boolean delete(int id) {
        conn= SQLServerDB.getConn();
        try {
            
            PreparedStatement pr = conn.prepareStatement(SQL_DELETE);
            pr.setInt(1, id);
            if (pr.executeUpdate() > 0) {
                setLastError("Delete Successful!");
                return true;
            } else {
                setLastError("Error: Delete Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public static void main(String[] args) {
        LocationTypeDAO dao = new LocationTypeDAO();
        ArrayList<LocationType> locationType = dao.readAll();
        LocationType l = new LocationType();
        l.setId(1);
        l.setName("Hue");
        l.setDescript("Hue");
        System.out.println(dao.create(l));
        System.out.println(dao.getLastError());
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }

}
